#include "iostream.h"为什么c[d][e]总是产生同样的数

来源:百度知道 编辑:UC知道 时间:2024/05/28 11:46:22
#include "iostream.h"
#include "stdlib.h"
void main()
{
int i,j,m,n,d,e,a[4][4],b[4][4],c[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
}

for(i=0;i<4;i++)
{ for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<"\n";
}
for(m=0;m<4;m++)
{
for(n=0;n<4;n++)
b[m][n]=rand()%35+101;
}
for(m=0;m<4;m++)
{for(n=0;n<4;n++)
cout<<b[m][n]<<' ';
cout<<"\n";
}
for(d=0;d<4;d++)
{for(e=0;e<4;e++)
c[d][e]=a[i][j]+b[m][n];
}
for(d=0;d<4;d++)
{for(e=0;e<4;e++)
cout<<c[d][e]<<' ';
cout<<"\n";
}
}

//设置种子数srand(time(NULL));修改如下:
#include "iostream.h"
#include "stdlib.h"
#include <time.h>
void main()
{
int i,j,m,n,d,e,a[4][4],b[4][4],c[4][4];
srand(time(NULL));
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
}

for(i=0;i<4;i++)
{ for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<"\n";
}
for(m=0;m<4;m++)
{
for(n=0;n<4;n++)
b[m][n]=rand()%35+101;
}
for(m=0;m<4;m++)
{for(n=0;n<4;n++)
cout<<b[m][n]<<' ';
cout<<"\n";
}
for(d=0;d<4;d++)
{for(e=0;e<4;e++)
c[d][e]=a[i][j]+b[m][n];
}
for(d=0;d<4;d++)
{for(e=0;e<4;e++)
cout<<c[d][e]<<' ';
cout<<"\n";
}
}

高人

rand()产生的是一组伪随机数,